package com.zmt.mrms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.springframework.stereotype.Service;

import com.zmt.mrms.entity.HospitalSection;
import com.zmt.mrms.entity.MedicalRecord;
import com.zmt.mrms.entity.PaymentRecord;
import com.zmt.mrms.entity.PrescriptionRecord;
import com.zmt.mrms.entity.UserAdmin;
import com.zmt.mrms.entity.UserDoctor;
import com.zmt.mrms.entity.UserPatient;
import com.zmt.mrms.mapper.AdminMapper;
import com.zmt.mrms.utils.ToolUtils;
import com.zmt.mrms.utils.VerifyUtils;

import jakarta.annotation.Resource;

@Service
public class AdminService {

	@Resource
	private AdminMapper adminMapper;

	VerifyUtils verifyTool = new VerifyUtils();
	ToolUtils toolUtils = new ToolUtils();

	/**
	 * 患者管理模块方法
	 **/

	public List<Map<String, Object>> redicalRecordManagement() {
		List<Map<String, Object>> fatherList = new ArrayList<>();
		List<MedicalRecord> medicalRecords = adminMapper.findAllMedicalRecords();
		for (int i = 0; i < medicalRecords.size(); i++) {
			Map<String, Object> tempMap = new HashMap<>();
			MedicalRecord medicalRecord = medicalRecords.get(i);
			PrescriptionRecord prescriptionRecord = adminMapper.findPreRecordByMedRecId(medicalRecord.getId());
			PaymentRecord paymentRecord = adminMapper.findPayRecordByMedRecId(medicalRecord.getId());
			tempMap.put("medicalList", medicalRecord);
			tempMap.put("prescriptionList", prescriptionRecord);
			tempMap.put("paymentList", paymentRecord);
			fatherList.add(tempMap);
		}
		return fatherList;
	}

	public int changePatientStatus(int id, boolean status) {
		return adminMapper.changePatientStatus(id, status);
	}

	public List<UserPatient> getPatient(String fuzzyQuery) {
		if (fuzzyQuery != null && fuzzyQuery.trim().length() != 0) {
			return adminMapper.findSomePatient("%" + fuzzyQuery + "%");
		}
		return adminMapper.findAllPatient();
	}

	/**
	 * 医师管理模块方法
	 **/

	public UserDoctor updateDoctor(UserDoctor userDoctor) {
		adminMapper.updateDoctor(userDoctor);
		return userDoctor;
	}

	public UserDoctor addDoctor(UserDoctor userDoctor) {
		String newAccount = toolUtils.getRandomString(8);
		String newPwd = toolUtils.getRandomString(8);
		userDoctor.setAccount(newAccount);
		userDoctor.setPassword(newPwd);
		adminMapper.addDoctor(userDoctor);
		return userDoctor;
	}

	public UserDoctor findOneDoctor(int id) {
		return adminMapper.findOneDcotor(id);
	}

	public int changeDoctorStatus(int id, boolean status) {
		return adminMapper.changeDoctorStatus(id, status);
	}

	public List<UserDoctor> getLimitedDoctor(String section, String position, String fuzzyQuery) {
		List<UserDoctor> limitedDoctors;
		if (fuzzyQuery != null && fuzzyQuery.trim().length() != 0) {
//			System.out.println("走的模糊查询");
			limitedDoctors = adminMapper.findFuzzyDoctor("%" + fuzzyQuery + "%");
		} else {
//			System.out.println("走的返回全部");
			limitedDoctors = getAllDoctor();
		}
		Iterator<UserDoctor> iterator = limitedDoctors.iterator();
		if (section != null && !section.equals("null")) {
//			System.out.println("111111111");
			while (iterator.hasNext()) {
				if (!section.equals(iterator.next().getSection())) {
					iterator.remove();
				}
			}
		}
		if (position != null && !position.equals("null")) {
//			System.out.println("222222222222");
			for (int i = 0, len = limitedDoctors.size(); i < len; i++) {
				if (!position.equals(limitedDoctors.get(i).getPosition())) {
					limitedDoctors.remove(i);
					len--;
					i--;
				}
			}
		}
		return limitedDoctors;
	}

	public Map<String, Set<String>> getFilterList() {
		List<HospitalSection> sectionList = adminMapper.findAllSection();
		Set<String> sectionSet = new TreeSet<>();
		for (int i = 0; i < sectionList.size(); i++) {
			sectionSet.add(sectionList.get(i).getName());
		}
		List<UserDoctor> doctorList = getAllDoctor();
		Set<String> positionSet = new TreeSet<>();
		for (int i = 0; i < doctorList.size(); i++) {
			positionSet.add(doctorList.get(i).getPosition());
		}
		Map<String, Set<String>> filterMap = new HashMap<>();
		filterMap.put("sectionList", sectionSet);
		filterMap.put("positionList", positionSet);
		return filterMap;
	}

	public List<UserDoctor> getAllDoctor() {
		return adminMapper.findAllDoctor();
	}

	/**
	 * 管理员个人信息管理模块方法
	 **/

	public int resetAdminAccount(int id) {
		String newAccount = toolUtils.getRandomString(10);
		String newPwd = toolUtils.getRandomString(10);
		return adminMapper.resetAccount(newAccount, newPwd, id);
	}

	public UserAdmin updateAdminInfo(String account, String pwd, int id) {
		adminMapper.updateAccountInfo(account, pwd, id);
		UserAdmin userAdmin = getAdminInfo(id);
		return userAdmin;
	}

	public UserAdmin getAdminInfo(int id) {
		UserAdmin userAdmin = adminMapper.findAdminbyId(id);
		return userAdmin;
	}

	/**
	 * 管理员登录模块方法
	 **/

	public String activateAccount(String idcard, int id) {
		if (verifyTool.isValidIDCard(idcard)) {
			adminMapper.updateIdcard(idcard, id);
			return "success";
		} else {
			return "该身份证不合法，请再次检查";
		}
	}

	public String loginByAdmin(UserAdmin userAdmin) {
		userAdmin = adminMapper.findAdmin(userAdmin);
		System.out.println(userAdmin);
		if (userAdmin == null) {
			return "您的密码或账号错误";
		} else {
			if (!userAdmin.isStatus() && userAdmin.getIdcard() != null) {
				return "该账号已被冻结，请找相关负责人处理";
			} else if (!userAdmin.isStatus() && userAdmin.getIdcard() == null) {
				return userAdmin.getId() + "/ADMIN/" + userAdmin.getSection() + "/" + userAdmin.getName() + "/activate";
			} else {
				return userAdmin.getId() + "/ADMIN/" + userAdmin.getSection() + "/" + userAdmin.getName() + "/success";
			}
		}
	}

}
